package com.example.myapplication3;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
public class MyAdapter extends BaseAdapter {

    //ArrayAdapter只接受List<String>
    //SimpleAdapter只接受List<Map<String,Object>>
    //BaseAdapter接收的数据List内的数据格式，由子类决定
    private List<ContactModel> dataList;
    private Context context;
    private OnContactItemIconClick listener;

    public MyAdapter(Context context, List<ContactModel> dataList) {
        this.dataList = dataList;
        this.context = context;
    }

    public void setListener(OnContactItemIconClick listener) {
        this.listener = listener;
    }

    //步骤1：ListView的长度在此处与数据List长度建立联系
    @Override
    public int getCount() {
        return null != dataList ? dataList.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return 0;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    //步骤2：条目布局与ListView显示内容建立关系
    //步骤3：getView()中的参数对应数据List的角标
    //getView()会随着列表滚动而不断的重复调用
    //设4：参数
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (null == convertView) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_contact, null);
            holder = new Holder();
            holder.contactIcon = convertView.findViewById(R.id.list_item_contact_icon);

            holder.contactName = convertView.findViewById(R.id.list_item_contact_name);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.contactIcon.setImageResource(dataList.get(position).getIconResId());
        holder.contactName.setText(dataList.get(position).getContactName());
        //添加条目中某个控件的点击事件
        holder.contactIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != listener) {
                    listener.onContactIconClick(position);
                }
            }
        });
        return convertView;
    }

    public interface OnContactItemIconClick {

        void onContactIconClick(int i);
    }

    public class Holder {

        ImageView contactIcon;

        TextView contactName;
    }

}
        //布局填充器，将xml布局文件转化成View对象
        //参数2：布局是否仍有父布局
        //LayoutInflater.inflate()方法等同于new对象
        /*View item = LayoutInflater.from(context)
                .inflate(R.layout.list_item_contact,null);
        //条目布局的子控件
        ImageView icon = item.findViewById(R.id.list_item_contact_icon);
        TextView name = item.findViewById(R.id.list_item_contact_name);

        //List中的数据
        String contactName = dataList.get(position).getContactName();
        int contactIconResId = dataList.get(position).getIconResId();
        //通过赋值，使得条目布局的子控件，显示数据List中的数据
        name.setText(contactName);
        icon.setImageResource(contactIconResId);
        return item;
    }

}
*/